package com.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.commons.domain.ResponseData;
import com.crm.entity.CrmRole;
import com.crm.service.ICrmRoleService;
import com.crm.util.StringUtil;
import com.crm.vo.PageBean;
import com.crm.vo.SysRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author junxian,dou
 * @since 2024-11-27
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class CrmRoleController {

    @Autowired
    private ICrmRoleService roleService;

    @ApiOperation(value = "查询所有角色")
    @GetMapping("/listAll")
    @PreAuthorize("hasAuthority('system:role:query')")
    public ResponseData listAll(){
        List<CrmRole> roleList = roleService.list();
        return ResponseData.success(roleList);
    }

    /**
     * 根据条件分页查询角色信息
     * @param pageBean
     * @return
     */
    @ApiOperation(value = "分页查询角色")
    @PostMapping("/list")
    @PreAuthorize("hasAuthority('system:role:query')")
    public ResponseData list(@RequestBody PageBean pageBean){
        PageBean<CrmRole> rolePageBean = roleService.lsitPage(pageBean);
        return ResponseData.success(rolePageBean);
    }

    /**
     * 添加或者修改
     * @param sysRole
     * @return
     */
    @ApiOperation(value = "保存角色")
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('system:role:add')"+"||"+"hasAuthority('system:role:edit')")
    public ResponseData save(@RequestBody SysRole sysRole){
        roleService.savaOrUpdateById(sysRole);
        return ResponseData.success();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取角色详情")
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('system:role:query')")
    public ResponseData findById(@PathVariable(value = "id")Integer id){
        CrmRole byId = roleService.getCrmRole(id);
        return ResponseData.success(byId);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除角色")
    @Transactional
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('system:role:delete')")
    public ResponseData delete(@RequestBody Long[] ids){
        roleService.removeWithByIds(Arrays.asList(ids));
        return ResponseData.success();
    }

    /**
     * 获取当前角色的权限菜单ID集合
     * @param id
     * @return
     */
    @ApiOperation(value = "获取当前角色菜单集合")
    @GetMapping("/menus/{id}")
    @PreAuthorize("hasAuthority('system:role:menu')")
    public ResponseData menus(@PathVariable(value = "id")Integer id){

        List<Integer> menuIdList = roleService.hasMenusList(id);
        return ResponseData.success(menuIdList);
    }


    /**
     * 更新角色权限信息
     * @param id
     * @param menuIds
     * @return
     */
    @ApiOperation(value = "更新角色权限")
    @Transactional
    @PostMapping("/updateMenus/{id}")
    @PreAuthorize("hasAuthority('system:role:menu')")
    public ResponseData updateMenus(@PathVariable(value = "id")Integer id, @RequestBody Integer[] menuIds){
        roleService.updateMenus(id,menuIds);
        return ResponseData.success();
    }
}
